﻿using System;
using System.Drawing;
using System.Windows.Forms;
using Gma.QrCodeNet.Encoding;
using Gma.QrCodeNet.Encoding.Windows.Render;
using System.Drawing.Imaging;
using System.IO;
using Yc.QrCodeLib;
using Yc.QrCodeLib.Data;


namespace MakeBarCode
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        bool update = true;

        private void textBox1_TextChanged(object sender, EventArgs e)
        {
            if (update)
            {
                String url = textBox1.Text;
                if (!url.Equals(""))
                {
                    Bitmap pic = ToQR(url);
                    pictureBox1.Image = pic;
                }
            }
        }


        private void save_Click(object sender, System.EventArgs e)
        {
            if (pictureBox1.Image != null)
            {
                Bitmap pic = new Bitmap(pictureBox1.Image);
                SaveFileDialog saveFileDialog1 = new SaveFileDialog();
                saveFileDialog1.Filter = "png files(*.png)|*.png|All files(*.*)|*.*";    //设置文件类型 
                saveFileDialog1.FilterIndex = 2;                                     //设置默认文件类型显示顺序 
                saveFileDialog1.RestoreDirectory = true;                             //保存对话框是否记忆上次打开的目录 
                if (saveFileDialog1.ShowDialog() == DialogResult.OK)
                {
                    string localFilePath = saveFileDialog1.FileName.ToString();
                    Save(pic, localFilePath);
                }
            }
        }

        private void go_Click(object sender, System.EventArgs e)
        {
            try
            {
                if (!textBox1.Text.Equals("")) System.Diagnostics.Process.Start(textBox1.Text);
            }
            catch (Exception)
            {
                MessageBox.Show("请检查网址或路径是否正确");
            }
        }

        private void Open_Click(object sender, System.EventArgs e)
        {
            OpenFileDialog open = new OpenFileDialog();
            if (open.ShowDialog() == DialogResult.OK)
            {
                Image pic = Bitmap.FromFile(open.FileName);
                pictureBox1.Image = pic;
                update = false;
                String code = ToCode(new Bitmap(pic));
                textBox1.Text = code;
                update = true;
            }
        }


        #region 二维码功能函数

        /// <summary>
        /// 获取链接地址对应的二维码图像
        /// </summary>
        public static Bitmap ToQR(String url)
        {
            QrEncoder qrEncoder = new QrEncoder(ErrorCorrectionLevel.M);
            QrCode qrCode = qrEncoder.Encode(url);
            GraphicsRenderer render = new GraphicsRenderer(new FixedModuleSize(5, QuietZoneModules.Two), Brushes.Black, Brushes.White);
            DrawingSize size = render.SizeCalculator.GetSize(qrCode.Matrix.Width);
            Bitmap pic = new Bitmap(size.CodeWidth, size.CodeWidth);
            Graphics g = Graphics.FromImage(pic);
            render.Draw(g, qrCode.Matrix);
            return pic;
        }
        /// <summary>
        /// 将二维码图像转化为编码串
        /// </summary>
        public static string ToCode(Bitmap pic)
        {
            QRCodeDecoder decoder = new QRCodeDecoder();
            String decodedString = decoder.decode(new QRCodeBitmapImage(pic));

            return decodedString;
        }
           /// <summary>
        /// 保存图像
        /// </summary>
        public static void Save(Bitmap pic, String filePath)
        {
            if (File.Exists(filePath)) File.Delete(filePath);
            pic.Save(filePath, ImageFormat.Png);
        }

        #endregion

    }
}